﻿using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    [DataContract]
    public class WPersonnelGroup : NotificationObject
    {
        private int personnelGroupUID;
        private String personnelGroupName;

        [DataMember(Order = 0)]
        public int PersonnelGroupUID
        {
            get
            {
                return personnelGroupUID;
            }
            set
            {
                personnelGroupUID = value;
                RaisePropertyChanged("PersonnelGroupUID");
            }
        }

        [DataMember(Order = 1)]
        public String PersonnelGroupName
        {
            get
            {
                return personnelGroupName;
            }
            set
            {
                personnelGroupName = value;
                RaisePropertyChanged("PersonnelGroupName");
            }
        }

        public WPersonnelGroup()
        {
            PersonnelGroupUID = -1;
            PersonnelGroupName = "";
    }

        public WPersonnelGroup(LPersonnelGroup p)
        {
            PersonnelGroupUID = p.PersonnelGroupUID;
            PersonnelGroupName = p.PersonnelGroupName;
        }

        public static String GetPersonnelGroupName(int pGroupUID)
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.PersonnelGroup, EMethod.SelectAll, "");

            if (res.Status != "Success")
            {
                return null;
            }

            List<WPersonnelGroup> list = JSON.parse<List<WPersonnelGroup>>(res.Data1);
            foreach (var item in list)
            {
                if(item.PersonnelGroupUID == pGroupUID)
                {
                    return item.PersonnelGroupName.Trim();
                }
            }

            return null;
        }

        public static int GetPersonnelGroupUID(String pGroupName)
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.PersonnelGroup, EMethod.SelectAll, "");

            if (res.Status != "Success")
            {
                return -1;
            }

            List<WPersonnelGroup> list = JSON.parse<List<WPersonnelGroup>>(res.Data1);
            foreach (var item in list)
            {
                if (item.PersonnelGroupName.Trim() == pGroupName.Trim())
                {
                    return item.PersonnelGroupUID;
                }
            }

            return -1;
        }

        public static List<WPersonnelGroup> GetAll()
        {
            HttpResponse resPersonnelGroup = FridgeConnector.PostMessage(EStudy.PersonnelGroup, EMethod.SelectAll, "");
            return JSON.parse<List<WPersonnelGroup>>(resPersonnelGroup.Data1);
        }

        public static HttpResponse Insert(List<WPersonnelGroup> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.PersonnelGroup, EMethod.Insert, postData);
        }

        public static HttpResponse Update(List<WPersonnelGroup> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.PersonnelGroup, EMethod.Update, postData);
        }

        public static HttpResponse Delete(List<WPersonnelGroup> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.PersonnelGroup, EMethod.Delete, postData);
        }
    }
}
